from rest_framework import serializers

from resume.models import Resume


class CreateResumeSerializer(serializers.ModelSerializer):
    """
    创建新简历序列化器
    """
    GENDER_CHOICES = (
        (0, '男'),
        (1, '女')
    )
    gender = serializers.ChoiceField(choices=GENDER_CHOICES, label='性别', required=False, default=0)
    mobile = serializers.IntegerField(label='手机号')
    class Meta:
        model = Resume
        exclude = ('is_delete',)
        extra_kwargs = {
            'mobile': {
                'min_length': 11,
                'max_length': 11,
            }
        }

    def create(self, validated_data):
        """
        创建简历
        """
        validated_data['avatar_url'] = "img/yasuo.gif"
        validated_data['real_name'] = '未填写'
        validated_data['gender'] = '男'
        validated_data['birth_date'] = '1970-01-01'
        validated_data['personal_url'] = '未填写'
        validated_data['mobile'] = '未填写'
        validated_data['now_city'] = '未填写'
        validated_data['contact_address'] = '未填写'
        validated_data['email'] = '未填写'
        resume = super().create(validated_data)
        resume.save()
        return resume


class UserResumeDetailSerializer(serializers.ModelSerializer):
    """
    查看当前用户简历序列化器
    """
    class Meta:
        model = Resume
        fields =('id','real_name','gender','birth_date','personal_url','mobile','email','now_city','contact_address','progress','avatar_url','school','city','company')


class UserResumeUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Resume
        fields = ('real_name', 'mobile', 'email', 'personal_url', 'avatar_url', 'now_city', 'contact_address', )

    def update(self, instance, validated_data):
        """更新，instance为要更新的对象实例"""
        instance.real_name = validated_data.get('real_name', instance.real_name)
        instance.mobile = validated_data.get('mobile', instance.mobile)
        instance.email = validated_data.get('email', instance.email)
        instance.personal_url = validated_data.get('personal_url', instance.personal_url)
        instance.avatar_url = validated_data.get('avatar_url', instance.avatar_url)
        instance.contact_address = validated_data.get('contact_address', instance.contact_address)
        instance.now_city = validated_data.get('now_city', instance.now_city)
        instance.save()
        return instance